//
//  ZYGesturePasswordVerifyVC.m
//  YiLin
//
//  Created by Zyark on 2018/6/22.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYGesturePasswordVerifyVC.h"
#import "PCCircleView.h"
#import "PCCircleViewConst.h"
#import "PCLockLabel.h"
#import "PCCircleInfoView.h"
#import "PCCircle.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface ZYGesturePasswordVerifyVC ()<CircleViewDelegate>
/**
 *  提示Label
 */
@property (nonatomic, strong) PCLockLabel *msgLabel;
/**
 *  解锁界面
 */
@property (nonatomic, strong) PCCircleView *lockView;
/**
 *  infoView
 */
@property (nonatomic, strong) PCCircleInfoView *infoView;

@property (nonatomic,strong) ZYChangeLanguage *language;
@end

@implementation ZYGesturePasswordVerifyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    _language = [ZYChangeLanguage sharedZYChangeLanguage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_VERIFY_GESTURE_PASSWORD];
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setHidesBackButton:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
}

#pragma mark - init

- (void)setupView
{
    [self setupCircleClockUI];
}

- (void)setupCircleClockUI
{
    CGFloat padding = 20;
    PCCircleInfoView *infoView = [[PCCircleInfoView alloc] init];
    infoView.frame = CGRectMake(0, 0, CircleRadius * 2 * 0.8, CircleRadius * 2 * 0.8);
    [self.view addSubview:infoView];
    infoView.centerX = self.view.centerX;
    infoView.top = self.navigationView.bottom + 10;
    
    PCLockLabel *msgLabel = [[PCLockLabel alloc] init];
    msgLabel.frame = CGRectMake(0, 0, kScreenW, 14);
    msgLabel.top = infoView.bottom + padding;
    msgLabel.centerX = self.view.centerX;
    [self.view addSubview:msgLabel];
    
    // 解锁界面
    PCCircleView *lockView = [[PCCircleView alloc] init];
    lockView.delegate = self;
    [self.view addSubview:lockView];
    lockView.top = msgLabel.bottom + padding;
    lockView.centerX = self.view.centerX;
    lockView.arrow = [ZYConfig isDisplayTrack];
    
    _infoView = infoView;
    _msgLabel = msgLabel;
    _lockView = lockView;
    
    [_lockView setType:CircleViewTypeVerify];
    [_msgLabel showNormalMsg:[_language getStringByKey:LC_ME_MAIN_MODIFY_GESTURE_PASSWORD_DEFAULT_TEXT]];
}

//初始化重设按钮
-(void)setupNavigationItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:[_language getStringByKey:LC_ME_MAIN_MODIFY_RESET_BTN_TITLE] style:UIBarButtonItemStylePlain target:self action:@selector(didClickRightItem)];
    [item setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = item;
}

#pragma -mark 重设按钮点击
- (void)didClickRightItem {
    // 1.隐藏按钮
    self.navigationItem.rightBarButtonItem.title = nil;
    // 2.infoView取消选中
    [self infoViewDeselectedSubviews];
    // 3.msgLabel提示文字复位
    [self.msgLabel showNormalMsg:[_language getStringByKey:LC_ME_MAIN_MODIFY_GESTURE_PASSWORD_DEFAULT_TEXT]];
    // 4.清除之前存储的密码
    [PCCircleViewConst saveGesture:nil Key:gestureOneSaveKey];
}

#pragma mark - infoView展示方法
#pragma mark - 让infoView对应按钮选中
- (void)infoViewSelectedSubviewsSameAsCircleView:(PCCircleView *)circleView {
    for (PCCircle *circle in circleView.subviews) {
        
        if (circle.state == CircleStateSelected || circle.state == CircleStateLastOneSelected) {
            
            for (PCCircle *infoCircle in self.infoView.subviews) {
                if (infoCircle.tag == circle.tag) {
                    [infoCircle setState:CircleStateSelected];
                }
            }
        }
    }
}

#pragma mark - 让infoView对应按钮取消选中
- (void)infoViewDeselectedSubviews {
    [self.infoView.subviews enumerateObjectsUsingBlock:^(PCCircle *obj, NSUInteger idx, BOOL *stop) {
        [obj setState:CircleStateNormal];
    }];
}

#pragma mark - circleView - delegate - login or verify gesture
- (void)circleView:(PCCircleView *)view type:(CircleViewType)type didCompleteLoginGesture:(NSString *)gesture result:(BOOL)equal{
    // 此时的type有两种情况 Login or verify
    if (equal) {
        NSLog(@"登陆成功！");
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        NSLog(@"密码错误！");
        [self.msgLabel showWarnMsgAndShake:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_GESTURE_PASSWORD_VERIFY_ERROR]];
    }
}

@end
